import _ from "lodash";
import { BaseDrawInfo } from "..";
import { drawerAop } from "../drawer-aop";

export interface TextInfo {
  text: string;
  x: number;
  y: number;
  fontSize?: number;
  fontFamily?: string;
}

export interface TextDrawInfo extends BaseDrawInfo, TextInfo {}

export class TextHelper {
  static _helper: TextHelper = new TextHelper();

  static getInstance() {
    return this._helper;
  }

  protected constructor() {}

  @drawerAop()
  draw(ctx: CanvasRenderingContext2D, drawInfo: TextDrawInfo) {
    if (!_.isNil(drawInfo.fontSize) || !_.isNil(drawInfo.fontFamily)) {
      // 10px sans-serif
      const fontSize = drawInfo.fontSize || 18;
      const fontFamily = drawInfo.fontFamily ?? "sans-serif";
      ctx.font = `${fontSize}px ${fontFamily}`;
    }

    if (drawInfo.strokeStyle) {
      ctx.strokeText(drawInfo.text, drawInfo.x, drawInfo.y);
    }
    if (drawInfo.fillStyle || !drawInfo.strokeStyle) {
      ctx.fillText(drawInfo.text, drawInfo.x, drawInfo.y);
    }
  }
}

export const text = TextHelper.getInstance();
